package com.anjho.pojo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "address")
@NamedQueries(value = {
		@NamedQuery(name = "Address.getAddressList", query = "SELECT a FROM Address a"),
		@NamedQuery(name = "Address.getAddressListByZip", query = "SELECT a FROM Address a WHERE a.zip = :zip"),
		@NamedQuery(name = "Address.getAddressByAdCode", query = "SELECT a FROM Address a WHERE a.adcode = :adcode")
})
public class Address implements Serializable {

	private static final long serialVersionUID = -6783518220349164154L;

	public Address(){
		this.setAdcode(0L);
		this.setNeighborhood(new Neighborhood());
		this.setComplement("");
		this.setName("");
		this.setZip("");
	}
	
	@Id
	@Column(name="adcode")
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	@OneToMany(mappedBy="address")
	private Long adcode;
	
	@ManyToOne
	@JoinColumn(name="nhcode", nullable=false)
	private Neighborhood neighborhood;
	
	@Column(name="zip", length=8, nullable=false)
	private String zip;
	
	@Column(name="name", nullable=false, length=72)
	private String name;
	
	@Column(name="complement", nullable=false, length=72)
	private String complement;

	public String getFormattedZip(){
		return this.zip.substring(0,5) + "-" + this.zip.substring(5,8);
	}
	
	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public Neighborhood getNeighborhood() {
		return neighborhood;
	}

	public void setNeighborhood(Neighborhood neighborhood) {
		this.neighborhood = neighborhood;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getComplement() {
		return complement;
	}

	public void setComplement(String complement) {
		this.complement = complement;
	}

	public Long getAdcode() {
		return adcode;
	}

	public void setAdcode(Long adcode) {
		this.adcode = adcode;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((adcode == null) ? 0 : adcode.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Address other = (Address) obj;
		if (adcode == null) {
			if (other.adcode != null)
				return false;
		} else if (!adcode.equals(other.adcode))
			return false;
		return true;
	}

}
